//
//  BaseNavigationController.swift
//  jingxin
//
//  Created by Yuki on 2020/8/5.
//  Copyright © 2020 Zl. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController ,UINavigationControllerDelegate,UIGestureRecognizerDelegate {
    //重写
    override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self;
        
        /**  侧滑返回手势  **/
        if self.responds(to: #selector(getter: interactivePopGestureRecognizer)){
            self.interactivePopGestureRecognizer?.delegate = self;
        }
    }
    
    
    /**  导航栏代理方法  **/
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        /**  触发返回手势  **/
        if self.responds(to: #selector(getter: interactivePopGestureRecognizer)){
            self.interactivePopGestureRecognizer?.isEnabled = false;
        }
        
        /**  这里要判断个数为 大于0，切顺序不能弄错   **/
        if self.viewControllers.count > 0{
            viewController.hidesBottomBarWhenPushed = true;
        }
        super.pushViewController(viewController, animated: animated);
    }
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if navigationController .responds(to: #selector(getter: interactivePopGestureRecognizer)){
            navigationController.interactivePopGestureRecognizer?.isEnabled = true;
        }
        if navigationController.viewControllers.count == 1{
            navigationController.interactivePopGestureRecognizer?.delegate = nil;
            navigationController.interactivePopGestureRecognizer?.isEnabled = false;
        }
    }
    
    ///手势代理
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.navigationController?.viewControllers.count == 1{
            return false;
        }
        return true;
    }
    
    
    /// 控制器返回
    @objc func controllerBack(){
        self.popViewController(animated: true);
    }
}
